import type { RouteMeta, RouteRecordRaw } from "vue-router";
import router from '../router/index'
type IRoute = RouteRecordRaw & { id?: string };

// 定义全部路由：Admin下的子一级
const allRoutes: IRoute[] = [
    {
        id: "1",
        name: "/",
        path: "",
        redirect: to => {
            // 该函数接收目标路由作为参数
            // 相对位置不以`/`开头
            // 或 { path: 'profile'}
            return '/admin/home'
        },
    },
    {
        id: "1",
        name: "home",
        path: "home",
        component: () => import("../views/admin/home/home.vue")
    },
    {
        id: "2-1",
        name: "shoplist",
        path: "shoplist",
        component: () => import("../views/admin/shopping/list.vue")
    },
    {
        id: "2-2",
        name: "shoppage",
        path: "shoppage",
        component: () => import("../views/admin/shopping/page.vue")
    },
    {
        id: "2-3",
        name: "shopreco",
        path: "shopreco",
        component: () => import("../views/admin/shopping/recommend.vue")
    },
    {
        id: "3-1",
        name: "list",
        path: "list",
        component: () => import("../views/admin/banner/list.vue")
    },
    {
        id: "3-2",
        name: "add",
        path: "add",
        component: () => import("../views/admin/banner/add.vue")
    },
    {
        id: "4-1",
        name: "adminlist",
        path: "adminlist",
        component: () => import("../views/admin/administrator/adminlist.vue")
    },
    {
        id: "4-2",
        name: "adminadd",
        path: "adminadd",
        component: () => import("../views/admin/administrator/adminadd.vue")
    }
    ,
    {
        id: "5-1",
        name: "cart",
        path: "cart",
        component: () => import("../views/admin/cart/list.vue")
    }
    ,
    {
        id: "6-1",
        name: "orderedit",
        path: "orderedit",
        component: () => import("../views/admin/order/edit.vue")
    }
    ,
    {
        id: "6-2",
        name: "orderlist",
        path: "orderlist",
        component: () => import("../views/admin/order/list.vue")
    },
    {
        id: "7-1",
        name: "user",
        path: "user",
        component: () => import("../views/admin/user/userlist.vue")
    },
    {
        id: "8-1",
        name: "analyse",
        path: "analyse",
        component: () => import("../views/admin/analyse/index.vue")
    }
]


export default function getRoutes() {
    // console.log("getRoutes()")
    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedKeys = JSON.parse(grants);
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', '6-2', '7', '7-1', '8', '8-1'];
    }

    // 2、获取当前用户的路由配置
    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            router.addRoute("admin", { ...item });
        }
    })
    console.log("router.getRoutes()", router.getRoutes());
}
export const clearCurrentRoutes = () => {
    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedKeys = JSON.parse(grants);
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', '6-2', '7', '7-1', '8', '8-1'];
    }
    // 2、删除当前用户的路由配置
    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            //    router.addRoute("Admin",{...item});
            router.removeRoute(item.name as string);
        }
    })
}